package wineshop.controller.backend;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import wineshop.domain.Hotel;
import wineshop.exception.ApplicationException;
import wineshop.service.HotelService;
import wineshop.service.Impl.HotelServiceImpl;

public class HotelServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		String act = request.getParameter("act");

		if ("input_hotel".equals(act)) {

			request.getRequestDispatcher("jsps/backend/hotel/input_hotel.jsp").forward(request, response);

		} else if ("create_hotel".equals(act)) {
			Hotel hotel = new Hotel();
			hotel.setHotelName(request.getParameter("hotelname"));
			hotel.setHotelAddress(request.getParameter("hoteladdress"));
			hotel.setHotelPhone(request.getParameter("hotelphone"));
			hotel.setRoomAccount(Integer.parseInt(request.getParameter("roomaccount")));

			HotelService hotelService = new HotelServiceImpl();
			try {
				hotelService.CreateHotel(hotel);
				response.sendRedirect("HotelMgr?act=loadall");
			} catch (ApplicationException e) {
				e.printStackTrace();
				request.setAttribute("errMsg", e.getMessage());
				request.getRequestDispatcher("jsps/backend/hotel/input_hotel.jsp").forward(request,response);
			}

		} else if ("loadall".equals(act)) {
			HotelService hotelService = new HotelServiceImpl();
			List<Hotel> hotels = null;
			
			try {
				hotels = hotelService.GetAllHotel();
				request.setAttribute("hotels", hotels);
			} catch (ApplicationException e) {
				e.printStackTrace();
				request.setAttribute("errMsg", e.getMessage());

			}

			request.getRequestDispatcher("jsps/backend/hotel/list_hotel.jsp").forward(request, response);

		} else if ("preModify".equals(act)) {
			int hotelId = Integer.parseInt(request.getParameter("hotelId"));

			HotelService hotelService = new HotelServiceImpl();
			Hotel hotel = hotelService.GetHotelById(hotelId);

			request.setAttribute("hotel", hotel);
			request.getRequestDispatcher("jsps/backend/hotel/update_hotel.jsp").forward(request, response);

		} else if ("updateHotel".equals(act)) {
			Hotel hotel = new Hotel();
			hotel.setHotelId(Integer.parseInt(request.getParameter("hotelId")));
			hotel.setHotelName(request.getParameter("hotelName"));
			hotel.setHotelAddress(request.getParameter("hotelAddress"));
			hotel.setHotelPhone(request.getParameter("hotelPhone"));
			hotel.setRoomAccount(Integer.parseInt(request.getParameter("roomAccount")));

			HotelService hotelService = new HotelServiceImpl();
			try {
				hotelService.UpdateHotel(hotel);
				response.sendRedirect("HotelMgr?act=loadall");
			} catch (ApplicationException e) {
				e.printStackTrace();
				request.setAttribute("errMsg", e.getMessage());
				request.setAttribute("hotel",hotelService.GetHotelById(hotel.getHotelId()));
				request.getRequestDispatcher("jsps/backend/hotel/update_hotel.jsp").forward(request,response);
			}

		} else if ("remove".equals(act)) {
			int hotelId = Integer.parseInt(request.getParameter("hotelId"));

			HotelService hotelService = new HotelServiceImpl();

			try {
				hotelService.DelHotel(hotelId);
			} catch (ApplicationException e) {
				e.printStackTrace();
				request.getSession().setAttribute("errMsg", e.getMessage());
			}
			response.sendRedirect("HotelMgr?act=loadall");
		}

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}

}
